package cn.edu.xmu.orderpayment.payment.Dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.orderpayment.payment.mapper.PaymentPatternPoMapper;
import cn.edu.xmu.orderpayment.payment.model.bo.PaymentPattern;
import cn.edu.xmu.orderpayment.payment.model.po.PaymentPatternPo;
import cn.edu.xmu.orderpayment.payment.model.po.PaymentPatternPoExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Guanlin Wang 22920192204282
 * @date 2021/12/15
 */
@Repository
public class PaymentPatternDao {
    private static final Logger logger = LoggerFactory.getLogger(PaymentDao.class);
    @Autowired(required = false)
    PaymentPatternPoMapper paymentPatternPoMapper;

    /**
     * 获取支付渠道的所有状态
     * @param[]
     * @return PaymentPo
     */
    public ReturnObject getPaymentPatternStates(){
        List<Map<String,Object>> stateList=new ArrayList<>();
        for(PaymentPattern.State state : PaymentPattern.State.values()){
            Map<String, Object> temp = new HashMap<>(); // temp暂时存储code-name
            temp.put("code", state.getCode()); // 返回状态码
            temp.put("name", state.getDescription()); // 返回状态名
            stateList.add(temp);
        }
        return new ReturnObject<>(stateList);
    }

    /**
     * 通过状态获得支付渠道
     * 返回值类型为 PaymentPattern
     * @return
     */
    public ReturnObject getPayPatternByState(Byte code){
        PaymentPatternPoExample example = new PaymentPatternPoExample();
        PaymentPatternPoExample.Criteria criteria = example.createCriteria();
        //如果code不空, 比如要求VALID,那就通过criteria查询
        if(code != null){
            criteria.andStateEqualTo(code);
        }
        List<PaymentPatternPo> pos;
        try{
            pos = paymentPatternPoMapper.selectByExample(example);
            List<PaymentPattern> bos = new ArrayList<>();
            for(PaymentPatternPo po : pos){
                PaymentPattern bo = (PaymentPattern)cloneVo(po, PaymentPattern.class);
                bos.add(bo);
            }
            return new ReturnObject(bos);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

//    /**
//     * 通过id获取支付渠道的状态
//     * @param id
//     * @return
//     */
//    public ReturnObject getPaymentPatternStateById(Long id){
//        PaymentPatternPo po;
//        try{
//            po = paymentPatternPoMapper.selectByPrimaryKey(id);
//            ReturnObject ret = new ReturnObject(po);
//            return Common.getRetVo(ret, PaymentPattern.class);
//        }catch (Exception e){
//            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
//        }
//    }

    public ReturnObject getPaymentPatternById(Long patternId) {
        try{
            PaymentPatternPo po=paymentPatternPoMapper.selectByPrimaryKey(patternId);
            if(po==null)return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST,"该支付方式不存在");
            return new ReturnObject<>(cloneVo(po,PaymentPattern.class));
        }
        catch (Exception e){
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

}
